package cn.mavor.day0929;

import java.io.*;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class ConsoleToFile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String directoryPath = ""; // 这里可以替换为用户输入的目录路径
        File directory = new File(directoryPath);

        // 检查目录是否存在
        if (!directory.exists() || !directory.isDirectory()) {
            System.out.println("The specified path is not a valid directory.");
            return;
        }

        // 创建一个文件来保存输入的内容
        File file = new File(directory, "console_input.txt");
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file, true))) {
            while (true) {
                String input = scanner.nextLine();
                if ("quit".equalsIgnoreCase(input)) {
                    break;
                }

                // 获取当前时间并格式化为hh:mm:ss格式
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                String timestamp = sdf.format(new Date());

                // 将输入和时间戳写入文件
                writer.write(timestamp + " " + input);
                writer.newLine();

                System.out.println("Input saved. Type 'quit' to exit.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}